package com.chunuo.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chunuo.gulimall.product.entity.CategoryBrandRelationEntity;
import com.chunuo.gulimall.product.service.CategoryBrandRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.common.utils.PageUtils;
import com.chunuo.common.utils.Query;

import com.chunuo.gulimall.product.dao.CategoryDao;
import com.chunuo.gulimall.product.entity.CategoryEntity;
import com.chunuo.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> queryWithTree() {
        //查询全部
        List<CategoryEntity> all = baseMapper.selectList(null);

        return all.stream().filter(categoryEntity -> {
            //过滤条件，只要没有父节点的 均为一级菜单
            return categoryEntity.getParentCid() == 0;
        }).map(menu ->{
            //将过滤后的菜单遍历子菜单，如何返回
            menu.setChildrenList(getChildrens(menu,all));
            return menu;
        }).sorted((menu1,menu2) ->{
            //升序排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        //最后构建成collection
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 得到节点的父子菜单
     * @param catelogId
     * @return
     */
    @Override
    public Long[] findParentPath(Long catelogId) {
        List<Long[]> list = new ArrayList();
        findParent(catelogId,list);
        log.info("long[]:{}",list);
        return list.toArray(new Long[list.size()]);
    }

    @Transactional
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("catelog_id",category.getCatId()).set("catelog_name",category.getName());
        categoryBrandRelationService.update(wrapper);
    }

    /**
     * 遍历找到所有父节点 的id，存在list中
     * @param root
     * @param list
     */
    private void findParent(Long root,List list){
        CategoryEntity byId = this.getById(root);
        if(byId.getParentCid() != 0){
            findParent(byId.getParentCid(),list);
        }
        list.add(root);
    }

    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity>  all){

        return all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(menu ->{
            menu.setChildrenList(getChildrens(menu,all));
            return menu;
        }).sorted((menu1,menu2) ->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
    }

}